package org.bigpigman.el.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description
 * @Author 朱咸军
 * @Date 2019/12/19
 **/
public class StringTool {

    private static final Logger log = LoggerFactory.getLogger(StringTool.class);

    /**
     * 将驼峰式写法转换成蛇形写法
     * @return
     */
    public static String parseHumpCaseToSnakeCase(String name) {
        if (isNullOrEmpty(name)) {
            throw new StringToolException(ThreadTool.getCallInfo() + "<要转换的字符串不能为空>");
        }
        char[] chars = name.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            Character c = Character.valueOf(chars[i]);
            if (i != 0 && Character.isUpperCase(c)) {
                sb.append("_" + Character.toLowerCase(c));
            } else {
                sb.append(Character.toLowerCase(c));
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String x = "MarkToSer";
        System.out.println(StringTool.parseHumpCaseToSnakeCase(x));
    }

    public static boolean isNullOrEmpty(String name) {
        if (name != null && name.trim().length() > 0) {
            return false;
        }
        return true;
    }
}
